package com.fesine.activemq.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @description: mq生产者类
 * @author: Fesine
 * @createTime:2017/9/3 14:27
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/9/3 14:27
 */
public class AppProducer {
    //创建mq连接地址
    private static final String url = "tcp://127.0.0.1:61616";
    //设置队列名称
    private static final String queueName = "queue-test";

    public static void main(String[] args) throws JMSException {
        //1、创建连接工厂
        ConnectionFactory cf = new ActiveMQConnectionFactory(url);
        //2、创建连接
        Connection con = cf.createConnection();
        //3、启动连接
        con.start();
        //4、创建会话
        //4.1、两个参数：是否开启事务，应答模式
        Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //队列和生产者均由session创建
        //5、根据队列名字创建一个目标地址
        Destination dest = session.createQueue(queueName);
        //6、创建一个指定目标的生产者
        MessageProducer producer = session.createProducer(dest);
        for (int i = 0; i < 100; i++) {
            //7、创建消息
            TextMessage text = session.createTextMessage("test" + i);
            producer.send(text);
            System.out.println("发送消息:"+text.getText());
        }
        //8、最后关闭连接
        con.close();
    }
}
